package com.pb.b2b.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.pb.b2b.common.dao.impl.BaseDaoImpl;
import com.pb.b2b.dao.CategoryLanguageDao;
import com.pb.b2b.model.Category;
import com.pb.b2b.model.CategoryLanguage;
import com.pb.b2b.model.enums.Status;

@Repository
public class CategoryLanguageDaoImpl extends BaseDaoImpl implements CategoryLanguageDao {
	@Autowired
	SessionFactory sessionFactory;

	@Override
	public List<CategoryLanguage> getCategoryLanguages(Category category) {

		Criteria criteria = sessionFactory.openSession().createCriteria(CategoryLanguage.class);
		criteria.setFetchMode("language", FetchMode.JOIN);
		criteria.createAlias("language", "language");
		criteria.setFetchMode("category", FetchMode.JOIN);
		criteria.createAlias("category", "category");
		criteria.add(Restrictions.eq("category", category));
		criteria.add(Restrictions.eq("language.status", Status.AKTIVE));
		criteria.addOrder(Order.asc("category.parentCategory"));

		return criteria.list();
	}

	@Override
	public List<CategoryLanguage> getDefaultCategoryLanguages() {

		Criteria criteria = sessionFactory.openSession().createCriteria(CategoryLanguage.class);
		criteria.setFetchMode("language", FetchMode.JOIN);
		criteria.createAlias("language", "language");
		criteria.setFetchMode("category", FetchMode.JOIN);
		criteria.createAlias("category", "category");
		criteria.add(Restrictions.eq("language.active", "X"));
		criteria.addOrder(Order.asc("category.parentCategory"));

		return criteria.list();
	}
}
